package com.md24housecall.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.md24housecall.adapter.PartnerAdapter;
import com.md24housecall.manager.CacheManager;
import com.md24housecall.manager.SkypeManager;
import com.md24housecall.model.Partner;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;
import com.md24housecall.view.PagingListView;
import com.md24housecall.view.PagingListView.PagingListViewListener;

public class PartnerDetailActivity extends Activity implements PagingListViewListener, OnItemClickListener{
	
	private PagingListView lvPartner;
	private CacheManager cacheManager;
	private PartnerAdapter adapter;
	private GlobalConstant constant=GlobalConstant.getInstance();
	private int start=0;
	private int limit=constant.ITEMPARTNER_NO;
	private int total=0;
	private List<Partner> listPartners = new ArrayList<Partner>();
	private String categoryId="";
	private SkypeManager skype = SkypeManager.getInstance();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.partner_detail);
		setFinishOnTouchOutside(false);
		try{
			initialize();
			loadData();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	private void loadData(){
//		cacheManager.open();
		total=cacheManager.getTotalPartnersByCategory(categoryId);
		if(cacheManager.selectPartners(start, limit,categoryId)!=null){
			MyLog.d(GlobalConstant.TAG, "select partner: "+cacheManager.selectPartners(start, limit,categoryId).size());
			for(Partner partner:cacheManager.selectPartners(start, limit,categoryId)){
				listPartners.add(partner);
			}
		}else{
			total=1;
			Partner partner=new Partner();
			partner.setImageUrl(""+R.drawable.comingsoon_demo);
			listPartners.add(partner);
		}
		adapter.notifyDataSetChanged();
		MyLog.d(GlobalConstant.TAG, "total: "+total);
		if(total>listPartners.size()){
			lvPartner.setPullLoadEnable(true);
			lvPartner.setPullRefreshEnable(true);
		}else{
			lvPartner.setPullLoadEnable(false);
			lvPartner.setPullRefreshEnable(true);
		}
	}
	
	private void initialize(){
		cacheManager=CacheManager.getInstance(this);
		lvPartner=(PagingListView)findViewById(R.id.lvPartner);
		adapter=new PartnerAdapter(this, listPartners);
		lvPartner.setAdapter(adapter);
		lvPartner.setPullLoadEnable(true);
		lvPartner.setPullRefreshEnable(true);
		lvPartner.setPagingListViewListener(this);
		lvPartner.setOnItemClickListener(this);
		categoryId=getIntent().getExtras().getString(GlobalConstant.getInstance().KEYPARTNERDETAIL);
		MyLog.d(GlobalConstant.TAG,"category id: "+categoryId);
	}
	public void btnOk_Clicked(View v){
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		MyLog.d(GlobalConstant.TAG, "position partner detail: "+position);
		try{
			if(!listPartners.get(position-1).getSkypeName().equals(""))
				skype.callViaSkype(this, listPartners.get(position-1).getSkypeName());
		}catch (Exception e) {
			// TODO: handle exception
			MyLog.d(GlobalConstant.TAG, "onItemClick partner detail: "+e.getMessage());
		}
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		start+=constant.ITEMPARTNER_NO;
//		limit+=constant.ITEMPARTNER_NO;
		loadData();
		lvPartner.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		listPartners.clear();
		start=0;
//		limit=constant.ITEMPARTNER_NO;
		loadData();
		lvPartner.stopRefresh();
	}
	
}
